import 'dart:async';
import 'dart:convert';
import 'package:flutter/services.dart';

import 'index.dart';

class GPtomPayPlugin {
  static const MethodChannel _channel = const MethodChannel('gptompay_plugin');

  ///绑定服务
  static Future<BindServiceResponse?> bindServer(
      {required String gptomPackageName}) async {
    var result = await _channel.invokeMethod(
        ChannelConfig.BIND_SERVER, {"packageName": gptomPackageName});

    if (result == null) return null;
    return BindServiceResponse.fromJson(json.decode(result));
  }

  ///卸载服务
  static Future<bool> unbindServer() async {
    var result = await _channel.invokeMethod(ChannelConfig.UNBIND_SERVER);
    return result;
  }

  ///注册交易
  static Future<RegisterV2Response?> registerV2({String? clientId}) async {
    var result = await _channel
        .invokeMethod(ChannelConfig.REGISTER_V2, {"clientId": clientId});
    if (result == null) return null;
    return RegisterV2Response.fromJson(json.decode(result));
  }

  ///启动交易
  ///[amount] 交易金额
  ///[cancelMode] cancelMode  1 – Last transaction  1 – 最后一个事务 , 2 – Older transactions  2 – 较旧的交易
  ///[clientId] 客户端ID
  ///[originReferenceNum] 原始参考号
  ///[originTransactionID] 原始交易ID
  ///[printByPaymentApp] 是否gptom打印
  ///[tipAmount] 小费金额
  ///[transactionID] 交易ID
  ///[transactionType] 交易类型
  ///[email] 邮箱
  ///[phone] 电话
  ///[preferableReceiptType] 收据类型
  ///[tipCollect] 是否收集小费
  ///[currencyCode] 货币代码
  ///[redirectPackageName] 跳转的package
  static Future<StartTransV2Response?> startV2({
    int? amount,
    int? cancelMode,
    String clientId = '',
    String originReferenceNum = '',
    String originTransactionID = '',
    bool printByPaymentApp = false,
    int? tipAmount,
    required String transactionID,
    TransactionType transactionType = TransactionType.SALE,
    String email = '',
    String phone = '',
    PreferableReceiptType preferableReceiptType = PreferableReceiptType.QR,
    bool tipCollect = false,
    String currencyCode = 'EUR',
    String redirectPackageName = '',
  }) async {
    final requestMap = {
      "amount": amount,
      "cancelMode": cancelMode,
      "clientId": clientId,
      "originReferenceNum": originReferenceNum,
      "originTransactionID": originTransactionID,
      "printByPaymentApp": printByPaymentApp,
      "tipAmount": tipAmount,
      "transactionID": transactionID,
      "transactionType": transactionType.value,
      "email": email,
      "phone": phone,
      "preferableReceiptType": preferableReceiptType.value,
      "tipCollect": tipCollect,
      "currencyCode": currencyCode,
      "redirectPackageName": redirectPackageName,
    };
    var result =
        await _channel.invokeMethod(ChannelConfig.START_TRANS_V2, requestMap);
    if (result == null) return null;
    return StartTransV2Response.fromJson(json.decode(result));
  }

  ///查询状态
  static Future<StateResponse?> getState(
      {required String transactionId}) async {
    var result = await _channel
        .invokeMethod(ChannelConfig.STATE, {"transactionId": transactionId});
    if (result == null) return null;
    return StateResponse.fromJson(json.decode(result));
  }

  ///查询交易单详情
  static Future<TransactionResponse?> getTransactionDetail(
      {required String transactionId}) async {
    var result = await _channel.invokeMethod(
        ChannelConfig.GET_TRANSACTON_DETAIL, {"transactionId": transactionId});
    if (result == null) return null;
    return TransactionResponse.fromJson(json.decode(result));
  }
}
